package com.example.weather;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.qweather.sdk.bean.base.Lang;
import com.qweather.sdk.bean.base.Unit;
import com.qweather.sdk.bean.geo.GeoBean;
import com.qweather.sdk.bean.weather.WeatherNowBean;
import com.qweather.sdk.view.HeConfig;
import com.qweather.sdk.view.QWeather;

public class MainActivity extends AppCompatActivity {
    TextView txt_temp,txt_weather;
    Button btn_qure;
    EditText txt_city;
    String city,city_id,now_weather,now_temp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txt_temp = (TextView) findViewById(R.id.txt_temp);
        txt_weather = (TextView) findViewById(R.id.txt_weather);
        btn_qure = (Button) findViewById(R.id.btn_qure);
        txt_city = (EditText) findViewById(R.id.txt_city);
        HeConfig.init("HE2204190927111385", "f6c3ed98e0714bd7b36eb5b328a50b0c"); //初始化
        HeConfig.switchToDevService();  //切换至开发版服务

        btn_qure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                city = txt_city.getText().toString().trim(); //获取输入的城市

                QWeather.getGeoCityLookup(MainActivity.this,city, new QWeather.OnResultGeoListener() {
                    @Override
                    public void onError(Throwable throwable) {
                        Toast.makeText(MainActivity.this, "失败",Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onSuccess(GeoBean geoBean) {
                        city_id = geoBean.getLocationBean().get(0).getId();
                    }
                });
                QWeather.getWeatherNow(MainActivity.this, city_id, Lang.ZH_HANS, Unit.METRIC, new QWeather.OnResultWeatherNowListener() {
                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(MainActivity.this, "第二个失败",Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onSuccess(WeatherNowBean weatherBean) {
                        now_temp = weatherBean.getNow().getTemp();
                        now_weather = weatherBean.getNow().getText();
                        txt_temp.setText(now_temp);
                        txt_weather.setText(now_weather);
                    }
                });
            }
        });


    }
}